<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ad extends CIC_A_Controller
{
	var $category;
	function __construct()
	{
		parent::__construct();
		$this->init();
	}
	private function init()
	{
		//获取广告分类
		$this->category = $this->cic_model->get_all('ad_category');
	}
	function index()
	{
		$this->admin_priv(__METHOD__);
		$data['category']	= $this->category;
		$this->load->view('admin/ad/index',$data);
	}
	/**
	 * 显示广告添加页面
	 */
	function ad_add()
	{
		$data['category'] = $this->category;
		$this->load->view('admin/ad/ad_add',$data);
	}
	/**
	 * 添加广告
	 */
	function add_ad()
	{
		$this->admin_priv(__METHOD__);
		$this->validate('ad', 'admin/ad/ad_add');
		$data = array(
				'ad_pid'	=> $this->input->post('category_id'),
				'ad_desc'	=> $this->input->post('ad_desc'),
				'ad_img'	=> $this->input->post('ad_img'),
				'ad_link'	=> $this->input->post('ad_link'),
				'ad_sort'	=> $this->input->post('ad_sort'),
		);
		if($this->cic_model->insert('ad',$data))
			$this->message->msg('添加成功！',site_url('admin/ad/index'));
		else
			$this->message->msg('添加失败！',site_url('admin/ad/ad_add'));
	}
	/**
	 * 显示广告修改页面
	 */
	function ad_edit()
	{
		$data['category']	= $this->category;
		$data['ad']			= $this->cic_model->get_one('ad',array('ad_id'=>$this->uri->segment(4)));
		$this->load->view('admin/ad/ad_edit',$data);
	}
	/**
	 * 修改广告
	 */
	function edit_ad()
	{
		$this->admin_priv(__METHOD__);
		$data = array(
				'ad_img'	=> $this->input->post('ad_img'),
				'ad_link'	=> $this->input->post('ad_link'),
				'ad_sort'	=> $this->input->post('ad_sort'),
				'ad_desc'	=> $this->input->post('ad_desc'),
		);
		if($this->cic_model->update('ad',$data,array('ad_id'=>$this->uri->segment(4))))
		{
			$this->message->msg('修改成功！',site_url('admin/ad/index'));
		}else{
			$this->message->msg('修改失败！',site_url('admin/ad/ad_edit'));
		}
	}
	/**
	 * 显示广告样式配置页面
	 */
	function config()
	{
		$this->admin_priv(__METHOD__);
		$data['category'] = $this->category;
		$this->load->view('admin/ad/config',$data);
	}
	/**
	 * 修改广告样式配置
	 */
	function update_config()
	{
		$this->admin_priv(__METHOD__);
		$this->validate('ad_style', 'admin/ad/config');
		if($this->cic_model->update('ad_style',array('ad_width'=>$this->input->post('ad_width'),'ad_height'=>$this->input->post('ad_height'),'ad_style'=>$this->input->post('ad_style')),array('category_id'=>$this->input->post('category_name'))))
		{
			$this->message->msg('修改成功！',site_url('admin/ad/config'));
		}else{
			$this->message->msg('未做任何修改！',site_url('admin/ad/config'));
		}
	}
	/**
	 * 广告分类显示
	 */
	function category()
	{
		$this->admin_priv(__METHOD__);
		$data['category'] = $this->category;
		$this->load->view('admin/ad/category',$data);
	}
	/**
	 * 广告分类添加页面显示
	 */
	function category_add()
	{
		$this->load->view('admin/ad/category_add');
	}
	/**
	 * 广告分类修改页面显示
	 */
	function category_edit()
	{
		$data['category'] = $this->cic_model->get_one('ad_category',array('category_id'=>$this->uri->segment(4)));
		$this->load->view('admin/ad/category_edit',$data);
	}
	/**
	 * 广告分类添加
	 */
	function add_category()
	{
		$this->admin_priv(__METHOD__);
		if($this->input->post('submit'))
		{
			$this->validate('ad_category','admin/ad/category_add');
			$result = $this->cic_model->insert('ad_category',array('category_name'=>$this->input->post('category_name')));
			if($result)
			{
				if($this->cic_model->insert('ad_style',array('category_id'=>$result)))
				{
					$this->message->msg('添加成功！',site_url('admin/ad/category'));
				}else{
					$this->message->msg('分类添加成功！样式默认参数失败！',site_url('admin/ad/category'));
				}
			}else{
				$this->message->msg('添加失败！',site_url('admin/ad/category_add'));
			}
		}
	}
	function delete_category()
	{
		$this->admin_priv(__METHOD__);
		if($this->cic_model->delete('ad_category',array('category_id'=>$this->uri->segment(4)))){
			$this->cic_model->delete('ad',array('ad_pid'=>$this->uri->segment(4)));
			if($this->input->is_ajax_request())
			{
				echo 1;
			}
		}else{
			$this->message->msg('添加成功！',site_url('admin/ad/category'));
		}
	}
	function update_category()
	{
		$this->admin_priv(__METHOD__);
		if($this->uri->segment(4))
		{
			$this->validate('ad_category','admin/ad/category_edit/'.$this->uri->segment(4));
			if($this->cic_model->update('ad_category',$this->input->post(),array('category_id'=>$this->uri->segment(4))))
				$this->message->msg('修改成功！',site_url('admin/ad/category'));
			else
				$this->message->msg('未作任何修改！',site_url('admin/ad/category_edit/'.$this->uri->segment(4)));
		}
	}
	
	
	
	
	function get_ad_style()
	{
		if($this->input->is_ajax_request())
		{
			echo json_encode($this->cic_model->get_one('ad_style',array('category_id'=>$this->input->post('category_id'))));
		}
	}
	function get_ad()
	{
		if($this->input->is_ajax_request())
		{
			$result = $this->cic_model->get_all('ad',array('ad_pid'=>$this->input->post('category_id')));
			if($result)
			{
				foreach ($result->result() as $key=>$value)
				{
					$data[$key]['ad_id'] = $value->ad_id;
					$data[$key]['ad_pid'] = $value->ad_pid;
					$data[$key]['ad_desc'] = $value->ad_desc;
					$data[$key]['ad_img'] = $value->ad_img;
					$data[$key]['ad_link'] = $value->ad_link;
					$data[$key]['ad_sort'] = $value->ad_sort;
				}
				echo json_encode($data);
			}else{
				echo 0;
			}
		}
	}
	
	function delete_ad()
	{
		$this->admin_priv(__METHOD__);
		if($this->cic_model->delete('ad',array('ad_id'=>$this->input->post('ad_id'))))
		{
			if($this->input->is_ajax_request())	
			{
				echo 1;
			}
		}else{
			$this->message->msg('删除成功！',site_url('admin/ad/index'));
		}
	}
}